package com.tanhua.server.controller;

import com.tanhua.model.mongo.Visitors;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.MovementService;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author ck
 * @date 2021/11/13 18:36
 */

@RestController
@RequestMapping("/movements")
public class MovementController {


    @Autowired
    private MovementService movementService;


    @Autowired
    private CommentsService commentsService;
    /**
     * 发布动态
     */

    @PostMapping
    public ResponseEntity movements(Movement movement, MultipartFile[] imageContent) throws IOException {
        movementService.publishMovement(movement,imageContent);
        return ResponseEntity.ok(null);
    }

    /**
     * 查询我的动态
     * @return
     */
    @GetMapping("/all")
    public ResponseEntity findByUserId(Long userId,
                                       @RequestParam(defaultValue = "1") Integer page,
                                       @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = movementService.findByUserId(userId,page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 好友动态查询
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping
    public ResponseEntity movements(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pageResult = movementService.findFriends(page,pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 推荐动态查询
     * @param page
     * @return
     */
    @GetMapping("/recommend")
    public ResponseEntity recommend(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize){
        PageResult pr = movementService.findRecommend(page,pagesize);
        return ResponseEntity.ok(pr);
    }

    /**
     * 查询单个动态
     */

    @GetMapping("/{id}")
    public ResponseEntity findById(@PathVariable("id") String movementId){
        MovementsVo vo = movementService.findMovementById(movementId);
        return ResponseEntity.ok(vo);
    }
    /**
     * 点赞
     */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String movementId){
        Integer counts = commentsService.countLikes(movementId);
        return ResponseEntity.ok(counts);
    }

    /**
     * 取消点赞
     */
     @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String movementId){
         Integer counts = commentsService.dislikeComment(movementId);
         return ResponseEntity.ok(counts);
     }

    /**
     * 喜欢
     */
    @GetMapping("/{id}/love")
    public ResponseEntity love(@PathVariable("id") String movementId){
        Integer counts = commentsService.countLove(movementId);
        return ResponseEntity.ok(counts);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{id}/unlove")
    public ResponseEntity unlove(@PathVariable("id") String movementId){
        Integer counts = commentsService.unLove(movementId);
        return ResponseEntity.ok(counts);
    }

    /**
     * 谁看过我
     */
    @GetMapping("/visitors")
    public ResponseEntity visitors(){
        List<VisitorsVo> vos = movementService.queryVisitorsList();
        return ResponseEntity.ok(vos);
    }
}
